require 'rake/clean'
require 'find'
CLOBBER.include(
  'grace', 
  'juce',
  'Premake-3.5',
  'juce_1_45.zip',
  'premake-src-3.5.zip'
)

desc "Get latest source from SVN repository"
file "grace" do
  command = "svn co"
  url = "http://commonmusic.svn.sf.net/svnroot/commonmusic/trunk/grace"
  target = "grace"
  sh "#{command} #{url} #{target}"
  cp "premake.lua", "grace/build"
end

desc "Build Grace"
file "grace/build/linux/build/Release/Grace/bin/grace" => [ 
  "grace", 
  "juce/bin/libjuce.a", 
  "/usr/local/bin/premake", 
  "/usr/bin/csound",
  "/usr/bin/chicken" ] do
  cd "grace/build/linux"
  cp "../premake.lua", "."
  sh "premake --verbose --csound --target gnu"
  sh "make"
  cd "../../.."
end

desc "Install Grace"
file "grace/build/linux/build/Release/Grace/lib" => [
  "grace/build/linux/build/Release/Grace/bin/grace" ] do
  cd "grace/build/linux"
  cp "../install.sh", "."
  sh "./install.sh all"
  cd "../../.."
end

desc "Download Juce"
file "juce" do
  sh "wget http://internap.dl.sourceforge.net/sourceforge/juce/juce_1_45.zip"
  sh "unzip juce_1_45.zip"
  cp "juce_Config.h", "juce"
end

desc "Install Juce"
file "juce/bin/libjuce.a" => [ "juce" ] do
  cd "juce/build/linux"
  sh "make CONFIG=Release"
  cd "../../.."
end

desc "Download Premake"
file "Premake-3.5" do
  sh "wget http://prdownloads.sf.net/premake/premake-src-3.5.zip"
  sh "unzip premake-src-3.5.zip"
end

desc "Install Premake"
file "/usr/local/bin/premake" => [ "Premake-3.5", "/usr/bin/lua" ] do
  cd "Premake-3.5"
  sh "make"
  sh "sudo ln -sf #{Dir.getwd}/bin/premake /usr/local/bin/premake"
  cd ".."
end

desc "Install Lua"
file "/usr/bin/lua" do
  sh "sudo emerge -uvDN lua"
end

desc "Install CSound"
file "/usr/bin/csound" do
  sh "sudo emerge -uvDN csound"
end

desc "Install emacs"
file "/usr/bin/emacs" do
  sh "sudo emerge -uvDN emacs"
end

desc "Install chicken"
file "/usr/bin/chicken" do
  sh "sudo emerge -uvDN chicken"
end

desc "Install slime"
file "/usr/share/emacs/site-lisp/slime" do
  sh "sudo emerge -uvDN slime"
end
